<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<script type="text/javascript" src="js/jquery.itextclear.js"></script>
<link rel="stylesheet" media="screen" href="CSS/Tree.css" />
<link rel="stylesheet" media="screen" href="lib/datatables/css/vpad.css" />

<h1 class="page-title">绩效奖金管理 - 奖金额度${performance == null ? "添加" : "编辑"}</h1>
<div class="container_12 clearfix leading">
	<div class="side grid_12 leading">
		<form class="form has-validation" id="post_form">
			<s:if test="#request.performance.id > 0">
				<input type="hidden" name="performance.id" value="${performance.id}" />
			</s:if>
            <input type="hidden" name="performance.organization.id" value="${organizationId}" />
			<div class="clear"></div>
			<div class="clearfix">
				<label for="title" class="form-label">标准 <em>*</em></label>
				<div class="form-input">
					<input type="text" id="title" name="performance.title" value="${performance.title}" required="required" placeholder="请输入考核标准" />
				</div>
			</div>
			<div class="clearfix">
				<label for="scoreStart" class="form-label">分数区间 <em>*</em></label>
				<div class="form-input">
                    <input type="text" id="scoreEnd" name="performance.scoreEnd" value="${performance.scoreEnd}" required="required" placeholder="请输入0-100的分数上标" onblur="checkScore(this, 1)"/>
                    <input type="text" id="scoreStart" name="performance.scoreStart" value="${performance.scoreStart}" required="required" placeholder="请输入0-100的分数下标" onblur="checkScorePart(this)"/>
				</div>
			</div>
            <div class="clearfix">
                <label for="percentage" class="form-label">奖金额度 <em>*</em></label>
                <div class="form-input">
                    <input type="text" id="percentage" name="performance.percentage" value="${performance.percentage}" required="required" placeholder="请输入奖金额度，单位%"/>
                </div>
            </div>
			<div class="clearfix">
				<label class="form-label" for="remark">备注</label>
				<div class="form-input form-textarea">
					<span class="iTextClearButtonContainer">
                        <textarea placeholder="备注" rows="5" name="performance.remark" id="remark">${performance.remark}</textarea><a class="iTextClearButton" style="display: none;"></a>
                    </span>
				</div>
			</div>
			<div class="form-action clearfix">
				<button class="button" type="button" onclick="checkForm()">
					<s:if test="#request.performance.id > 0">修改</s:if>
					<s:else>添加</s:else>
				</button>
				<button class="button" type="reset">重置</button>
				<a class="button" href="javascript:void(0)"
					onclick="javascript:history.back()"><span
					style="background: url(${ctx}/images/img/arrow_undo.png)"
					class="icon">&nbsp;</span>返回</a>
			</div>
		</form>
	</div>
</div>
<div class="clear"></div>
<script type="text/javascript">
    function checkJJ(obj){
        var v = $(obj).val();
        if(checkNull(v)){
            alert("奖金额度不为空");
            return false;
        }
        if(isNaN(v)){
            alert("奖金额度只能是数字")
            $(obj).val("");
            return false;
        }
        return true;
    }
    function checkScore(obj, type){
        var v = $(obj).val();
        if(checkNull(v)){
            return false;
        }

        var title = (type == 1 ? "分数上标" : "分数下标");
        if(isNaN(v)){
            alert(title+"只能是数字")
            $(obj).val("");
            return false;
        }
        var vf = parseFloat(v);
        if(vf > 100 || vf < 0){
            alert(title+"应在0到100之间");
            $(obj).val("");
            return false;
        }
        return true;
    }
    //检测分数区间(下标输入完成时检查)
    function checkScorePart(obj){
        if(!checkScore($("#scoreEnd"), 1)){
            alert("上标没输入或输入不正确");
            return false;
        }
        if(!checkScore(obj, 0)){
            alert("下标没输入或输入不正确");
            return false;
        }
        var vf1 = parseFloat($("#scoreEnd").val());
        var vf2 = parseFloat($(obj).val());
        if(vf1 <= vf2){
            alert("上标必须大于下标数值，请正确输入");
            $("#scoreEnd").val("");
            $(obj).val("");
            return false;
        }

        //检测数据有效性
        var url = "${ctx}/salary/performance_check.xhtml";
        $.ajax(url, {
            type : "POST",
            data : {
                "organizationId":"${organizationId}",
                "performance.organization.id":eval("${performance.organization.id}"),
                "performance.scoreEnd":parseFloat($("#scoreEnd").val().trim()),
                "performance.scoreStart":parseFloat($("#scoreStart").val().trim()),
                "performance.id":eval("${performance.id}")
            },
            dataType : "json",
            success : function(data) {
                if(data == "success"){
                    return true;
                }else{
                    alert("输入错误或输入的分数区间跨越其他分数区间，请重新输入");
                    $("#scoreEnd").val("");
                    $(obj).val("");
                    return;
                }
            },
            error : function(XMLHttpRequest, textStatus, errorThrown) {
                alert('检测数据异常');
            },
            complete : function() {
            }
        });
    }

	function checkForm() {
		if ($("#title").val() == null) {
			alert("请填写标准名称");
			return;
		}
		if ($("#scoreStart").val() == null
				|| $("#scoreStart").val().trim() == ""
				|| isNaN($("#scoreStart").val())
				|| parseFloat($("#scoreStart").val().trim()) < 0 || parseFloat($("#scoreStart").val().trim()) > 100 ) {
			alert("请输入0-100的分数上标");
			return;
		}
		if ($("#scoreEnd").val() == null || $("#scoreEnd").val().trim() == ""
				|| isNaN($("#scoreEnd").val())
				|| parseFloat($("#scoreEnd").val().trim()) < 0 || parseFloat($("#scoreEnd").val().trim()) > 100) {
			alert("请输入0-100的分数下标");
			return;
		}
		
		if(parseFloat($("#scoreStart").val().trim()) >= parseFloat($("#scoreEnd").val().trim())){
			alert("下标不能大于上标,请重新输入");
			return;
		}

        if(!checkJJ($("#percentage"))){
            return;
        }

        var url = "${ctx}/salary/performance_save.xhtml";
        $.ajax(url, {
            type: "POST",
            data: $("#post_form").serialize(),
            dataType:"html",
            cache:false,
            success:function(a) {
                return pageDownloaded(a, url)
            },
            complete:function() {}
        });

	}
</script>
